﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using Model;
using Model.Entities;
using Model.Account;
using Model.Helpers;

namespace DAL
{
    public class SyncarContext : DbContext
    {
        public DbSet<Vehicle> Vehicles { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<UserVehicle> UserVehicles { get; set; }
        public DbSet<Publication> Publications { get; set; }
        public DbSet<Contract> Contracts { get; set; }
        public DbSet<Comment> Comments { get; set; }
        public DbSet<Image> Images { get; set; }

        public IQueryable<User> FindUserByUserName(String userName)
        {
            if (String.IsNullOrWhiteSpace(userName))
                throw new ArgumentException("username cannot be empty");

            return (from u in this.Users
                          where u.UserName == userName
                          select u);
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            modelBuilder.Entity<Image>().Ignore(t => t.EntityID);
            base.OnModelCreating(modelBuilder);
        }
    }
}
